package com.ydj.app.controller;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import com.ydj.app.protocol.http.cms.CmsCommonUploadFileRes;
import com.ydj.framework.annotation.AllowGuest;
import com.ydj.framework.exception.CommonException;
import com.ydj.framework.filter.ExtHttpServletRequest;
import com.ydj.framework.protocol.BaseEnum;
import com.ydj.framework.protocol.JsonResult;
import com.ydj.app.entity.CmsAccount;
import com.ydj.app.protocol.http.oldcms.CmsCommonGetProfileRes;
import com.ydj.app.protocol.http.oldcms.CmsCommonLoginReq;
import com.ydj.app.protocol.http.oldcms.CmsCommonLoginRes;
import com.ydj.app.service.CommonService;
import com.ydj.framework.third.oss.OssPath;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import java.io.IOException;
import java.io.InputStream;

@RestController
@RequestMapping("/cms/common")
@Api(tags = "CMS-公用")
public class CmsCommonController {
    @Autowired
    CommonService commonService;

    @AllowGuest
    @ApiOperation(value = "登录")
    @PostMapping(value = "/login")
    public JsonResult<CmsCommonLoginRes> login(@ApiIgnore ExtHttpServletRequest request, @RequestBody @Validated CmsCommonLoginReq req) throws CommonException {
        CmsCommonLoginRes res = commonService.cmsLogin(request, req);
        return JsonResult.success(res);
    }

    @ApiOperation(value = "获取账号信息")
    @PostMapping(value = "/getProfile")
    public JsonResult<CmsCommonGetProfileRes> getProfile(@ApiIgnore CmsAccount account) throws CommonException {
        CmsCommonGetProfileRes res = commonService.cmsGetProfile(account);
        return JsonResult.success(res);
    }

    @ResponseBody
    @ApiOperationSupport(order = 30)
    @ApiOperation(value = "上传文件")
    @PostMapping(value = "/uploadFile")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "cosPath", value = "路径的枚举(banner, 客服二维码csQrCode)", required = true, dataType = "string"),
            @ApiImplicitParam(name = "fileName", value = "文件名", required = true, dataType = "string"),
            @ApiImplicitParam(name = "uploadFile", value = "上传文件", required = true, dataType = "uploadFile")
    })
    public JsonResult<?> uploadFile(@ApiIgnore ExtHttpServletRequest request, @ApiIgnore CmsAccount account) throws IOException, CommonException {
        String ossPathString = request.getParameter("cosPath");
        String fileName = request.getParameter("fileName");
        OssPath cosPath = BaseEnum.getByText(OssPath.values(), ossPathString);
        CommonException.throwExceptionIfNull(cosPath, "路径枚举错误");

        InputStream uploadFileInputStream = request.getUploadFileStream("uploadFile");
        String uploadFileUrl = commonService.uploadFile(uploadFileInputStream, fileName, cosPath);
        uploadFileInputStream.close();

        CmsCommonUploadFileRes res = new CmsCommonUploadFileRes()
                .setSuccess(uploadFileUrl != null)
                .setUrl(uploadFileUrl);
        return JsonResult.success(res);
    }
}